using System.Web.Mvc;
using TfsReports.Web.Extensions;
using TfsReports.Web.Helpers;
using TfsReports.Web.Models;

namespace TfsReports.Web.Controllers
{
    public class ChangesetController : Controller
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ChangesetController"/> class.
        /// </summary>
        public ChangesetController()
        {
            TempDataProvider = new NoTempDataProvider();
        }

        /// <summary>
        /// Generates an Index view of <see cref="Changeset"/>
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        /// <example>GET: /Changeset</example>
        public ActionResult Index(int? page, int? pageSize)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            var changesets = TfsWarehouse.GetAllChangesets();
            ViewData["Title"] = "Changesets";
            changesets.Sort((c1, c2) => c1.Id.CompareTo(c2.Id));
            return View(changesets.ToPagedList(currentPageIndex, pageSize ?? 10));
        }

        /// <summary>
        /// Generates an Details view of <see cref="ChangesetController"/>
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        /// <example>GET: /Changeset/Details/5</example>
        public ActionResult Details(int id)
        {
            Churn churn = TfsWarehouse.GetAllChurns().Find(c => c.Changeset.Id == id);
            if (churn != null)
                return RedirectToAction("Details", "Churn", new { id = churn.Id });
            return View(TfsWarehouse.GetChangeset(id));
        }
    }
}
